package com.jari.lg;


import com.jari.lg.domain.DevBase;
import com.jari.lg.domain.SysDictData;
import com.jari.lg.mapper.*;
import com.jari.lg.utils.BaseData;
import com.jari.lg.utils.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ctl
 * @version 1.0
 * @description 启动
 * @date 2020/3/2 9:16
 */
@Component
@Slf4j
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {
    @Resource
    private DevBaseMapper devBaseMapper;
    @Resource
    private CrossBaseMapper crossBaseMapper;
    @Resource
    private ParkingLotBaseMapper parkingLotBaseMapper;
    @Resource
    private KeyAreaMapper keyAreaMapper;
    @Resource
    private AreaBaseMapper areaBaseMapper;
    @Resource
    private SysDictDataMapper sysDictDataMapper;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        try {
            //所有城域片区
            BaseData.areaBaseList = areaBaseMapper.selectByExample(null);
            //所有重点区域
            BaseData.keyAreaList = keyAreaMapper.selectByExample(null);
            //所有停车场
            BaseData.parkingLotBaseList = parkingLotBaseMapper.selectByExample(null);
            //所有交叉口
            BaseData.crossBaseList = crossBaseMapper.selectByExample(null);
            //所有设备
            BaseData.devBaseList = devBaseMapper.selectByExample(null);
            //所有字典
            BaseData.sysDictDataList = sysDictDataMapper.selectByExample(null);
            //设备类型字典
            BaseData.deviceTypeList = BaseData.sysDictDataList.stream().filter(x -> "DEVICE_TYPE".equals(x.getDicttype())).collect(Collectors.toList());
            //车牌类型字典
            BaseData.carPlateTypeList = BaseData.sysDictDataList.stream().filter(x -> "CAR_PLATE_TYPE".equals(x.getDicttype())).collect(Collectors.toList());
            //车辆类型字典
            BaseData.carTypeList = BaseData.sysDictDataList.stream().filter(x -> "KEY_CAR_TYPE".equals(x.getDicttype())).collect(Collectors.toList());
            //违法类型字典
            BaseData.violationTypeList = BaseData.sysDictDataList.stream().filter(x -> "VIO_TYPE".equals(x.getDicttype())).collect(Collectors.toList());
            //违法类型字典
            BaseData.directionList = BaseData.sysDictDataList.stream().filter(x -> "DIRECTION".equals(x.getDicttype())).collect(Collectors.toList());
            //路口类型字典
            BaseData.crossTypeList = BaseData.sysDictDataList.stream().filter(x -> "CROSS_TYPE".equals(x.getDicttype())).collect(Collectors.toList());
            //停车场类型字典
            BaseData.parkingLotTypeList = BaseData.sysDictDataList.stream().filter(x -> "PARKING_TYPE".equals(x.getDicttype())).collect(Collectors.toList());
            //电警
            List<SysDictData> tmpDeviceTypeList = BaseData.deviceTypeList.stream().filter(y -> BaseData.deviceType[0].equals(y.getDictlabel())).collect(Collectors.toList());
            if (tmpDeviceTypeList.size() != 1) {
                throw new Exception("字典表错误：不存在" + BaseData.deviceType[0] + "设备类型");
            } else {
                String ePoliceDictData = tmpDeviceTypeList.get(0).getDictdata();
                BaseData.ePoliceList = BaseData.devBaseList.stream()
                        .filter(x -> x.getDevicetype().equals(ePoliceDictData))
                        .collect(Collectors.toList());
            }
            //卡口
            tmpDeviceTypeList = BaseData.deviceTypeList.stream().filter(y -> BaseData.deviceType[1].equals(y.getDictlabel())).collect(Collectors.toList());
            if (tmpDeviceTypeList.size() != 1) {
                throw new Exception("字典表错误：不存在" + BaseData.deviceType[1] + "设备类型");
            } else {
                String checkpointDictData = tmpDeviceTypeList.get(0).getDictdata();
                BaseData.checkpointList = BaseData.devBaseList.stream()
                        .filter(x -> x.getDevicetype().equals(checkpointDictData))
                        .collect(Collectors.toList());
            }
            //视频检测器
            tmpDeviceTypeList = BaseData.deviceTypeList.stream().filter(y -> BaseData.deviceType[2].equals(y.getDictlabel())).collect(Collectors.toList());
            if (tmpDeviceTypeList.size() != 1) {
                throw new Exception("字典表错误：不存在" + BaseData.deviceType[2] + "设备类型");
            } else {
                String videoDictData = tmpDeviceTypeList.get(0).getDictdata();
                BaseData.videoDetectorList = BaseData.devBaseList.stream()
                        .filter(x -> x.getDevicetype().equals(videoDictData))
                        .collect(Collectors.toList());
            }
            //诱导屏
            tmpDeviceTypeList = BaseData.deviceTypeList.stream().filter(y -> BaseData.deviceType[3].equals(y.getDictlabel())).collect(Collectors.toList());
            if (tmpDeviceTypeList.size() != 1) {
                throw new Exception("字典表错误：不存在" + BaseData.deviceType[3] + "设备类型");
            } else {
                String guideDictData = tmpDeviceTypeList.get(0).getDictdata();
                BaseData.guideList = BaseData.devBaseList.stream()
                        .filter(x -> x.getDevicetype().equals(guideDictData))
                        .collect(Collectors.toList());
            }
            //判断车牌类型字典是否存在
            for (int i = 0; i < BaseData.carPlateType.length; i++) {
                int finalI = i;
                List<SysDictData> tmpCarPlateTypeList = BaseData.carPlateTypeList.stream().filter(y -> BaseData.carPlateType[finalI].equals(y.getDictlabel())).collect(Collectors.toList());
                if (tmpCarPlateTypeList.size() != 1) {
                    throw new Exception("字典表错误：不存在" + BaseData.carPlateType[i] + "车牌类型");
                }
            }
            //判断车辆类型字典是否存在
            for (int i = 0; i < BaseData.carType.length; i++) {
                int finalI = i;
                List<SysDictData> tmpCarTypeList = BaseData.carTypeList.stream().filter(y -> BaseData.carType[finalI].equals(y.getDictlabel())).collect(Collectors.toList());
                if (tmpCarTypeList.size() != 1) {
                    throw new Exception("字典表错误：不存在" + BaseData.carPlateType[i] + "车辆类型");
                }
            }
            //判断违法类型字典是否存在
            for (int i = 0; i < BaseData.violationType.length; i++) {
                int finalI = i;
                List<SysDictData> tmpViolationTypeList = BaseData.violationTypeList.stream().filter(y -> BaseData.violationType[finalI].equals(y.getDictlabel())).collect(Collectors.toList());
                if (tmpViolationTypeList.size() != 1) {
                    throw new Exception("字典表错误：不存在" + BaseData.violationType[i] + "违法类型");
                }
            }
            //判断方向字典是否存在
            for (int i = 0; i < BaseData.direction.length; i++) {
                int finalI = i;
                List<SysDictData> tmpDirectionList = BaseData.directionList.stream().filter(y -> BaseData.direction[finalI].equals(y.getDictlabel())).collect(Collectors.toList());
                if (tmpDirectionList.size() != 1) {
                    throw new Exception("字典表错误：不存在" + BaseData.direction[i] + "方向类型");
                }
            }
            //判断交叉口类型字典是否存在
            for (int i = 0; i < BaseData.crossType.length; i++) {
                int finalI = i;
                List<SysDictData> tmpCrossTypeList = BaseData.crossTypeList.stream().filter(y -> BaseData.crossType[finalI].equals(y.getDictlabel())).collect(Collectors.toList());
                if (tmpCrossTypeList.size() != 1) {
                    throw new Exception("字典表错误：不存在" + BaseData.crossType[i] + "交叉口类型");
                }
            }
            //判断停车场类型字典是否存在
            for (int i = 0; i < BaseData.parkingLotType.length; i++) {
                int finalI = i;
                List<SysDictData> parkingLotTypeList = BaseData.parkingLotTypeList.stream().filter(y -> BaseData.parkingLotType[finalI].equals(y.getDictlabel())).collect(Collectors.toList());
                if (parkingLotTypeList.size() != 1) {
                    throw new Exception("字典表错误：不存在" + BaseData.crossType[i] + "停车场类型");
                }
            }

        } catch (Exception ex) {
            log.error(LogUtil.getStackMsg(ex));
        }

    }


}
